import bot from "./amesu";

let replaceAll = function (find, replace, str) {
    var find = find.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
    return str.replace(new RegExp(find, 'g'), replace);
}
export class Bot_msg {
    private orignData: any;
    private auth: { avatar: string, bot: boolean, id: string, username: string, member_openid:string, } = {
        avatar: 'https://qqchannel-profile-1251316161.file.myqcloud.com/16772206391fde55516d0a7539?t=1677220640',
        bot: false,
        id: '10001',
        username: 'guest',
        member_openid: '1145141919810',
    };
    content: string = '';
    private guild_id: string = '';
    private channel_id: string = '';
    private msg_id: string = '';
    private createTime: number = Date.now();
    // 群消息
    private group_id: string = '';
    private group_openid: string = '';

    constructor(data: any) {
        this.orignData = data;

        switch (data.t) {
            case 'MESSAGE_CREATE':
                // 全局消息创建
                this.fix_overall(data);
                break;
            case 'AT_MESSAGE_CREATE':
                this.fix_overall(data);
                break;
            case 'GROUP_AT_MESSAGE_CREATE':
                this.fix_overall(data);
                break;
            default:
                break;
        }
    }
    at() {
        return 'test at'
    }
    getAuthId() {
        return this.auth.id
    }
    getGuild() {
        return this.guild_id;
    }
    getChannelid() {
        return this.channel_id;
    }
    getGroupId() {
        return this.group_id;
    }
    getFormId() {
        return this.channel_id ?? this.group_openid;
    }
    getMsgid() {
        return this.msg_id;
    }
    getHeader() {
        // 群无法获取头像，需要给默认值
        return this.auth.avatar;
    }
    getName() {
        // 群无法获取用户昵称，只有 member_openid 唯一标识
        return this.auth.username ?? '匿名轮回者';
    }
    getContent(real?) {
        if(real){
            return this.content;
        }
        // 去掉艾特机器人文字
        return this.content.toUpperCase();
    }
    /**
     * 获取消息创建时间
     * @returns 
     */
    getCreateTime() {
        return this.createTime;
    }
    /**
     * 
     * @param str 快速回复文本内容
     */
    repeText(str: string) {
        if (Date.now() - this.createTime > 1000 * 60 * 5) {
            console.log('消息过期');
            return;
        }
        bot.sendText(this.getFormId(), str);
    }
    private fix_overall(data: any) {
        this.content = data.content;
        const _bot = bot.getBotInfo();
        this.content = replaceAll(`<@!${_bot.id}>`, '', this.content)
        this.content = replaceAll('\n', '', this.content)
        this.content = replaceAll(' ', '', this.content)
        this.content = replaceAll('+', '', this.content)
        this.content = replaceAll('/', '', this.content)
        if (data.author) {
            this.auth = data.author;
        }
        this.guild_id = data.guild_id;
        this.channel_id = data.channel_id;
        this.msg_id = data.id;
        this.group_id = data.group_id;
        this.group_openid = data.group_openid;
    }
    // 发布事件
    emit() {

    }
}